package net.sf.rose.exception;

/**
 * 功能:所有异常的封装父类
 * @author fengjian
 * @since 2013/05/30
 */
public class SystemException extends RuntimeException {

	private static final long serialVersionUID = 1L;
	
	/** 异常类型码 */
	private ErrorCode errorCode;

	/** 异常附加文本信息 */
	private StringBuilder sb = null;

	/**
	 * 异常创造函数
	 * 
	 * @param message   - 文本消息
	 * @param errorCode - 异常类型码
	 */
	public SystemException(String message, ErrorCode errorCode) {
		super(message);
		this.errorCode = errorCode;
		this.addMessage(message);
	}
	
	/**
	 * 添加异常附加文本信息
	 */
	public void addMessage(String message) {
		if (sb == null) {
			sb = new StringBuilder();
			sb.append(message);
		} else {
			sb.append('\n').append(message);
		}
	}

	/**
	 * 返回异常附加文本信息
	 */
	public String getMessage() {
		return sb.toString();
	}

	/**
	 * 返回异常定义码
	 */
	public ErrorCode getErrorCode() {
		return errorCode;
	}

	/**
	 * 返回异常定义码的大类型
	 */
	public ErrorCode getErrorType() {
		int num = errorCode.getNumber();
		if (num < SystemErrorCode.DAO_ERROR.getNumber()) {
			return SystemErrorCode.DAO_ERROR;
		} else if (num < SystemErrorCode.SERVICE_ERROR.getNumber()) {
			return SystemErrorCode.SERVICE_ERROR;
		} else if (num < SystemErrorCode.APPLICATION_ERROR.getNumber()) {
			return SystemErrorCode.APPLICATION_ERROR;
		} else if (num < SystemErrorCode.USER_ERROR.getNumber()) {
			return SystemErrorCode.USER_ERROR;
		}
		return SystemErrorCode.USER_ERROR;
	}

}
